<?php
namespace App\Http\Controllers\Admin\Advertisement;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class AdvertisingPositionController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'advertisingposition'],
        'show'  => ['get', 'advertisingposition/%d'],
        'store' => ['post', 'advertisingposition'],
        'update'=> ['put', 'advertisingposition/%d'],
        'delete'=> ['delete', 'advertisingposition/%d'],
        'setShow'=> ['post', 'advertisingposition/%d'],
    ];

    protected $request;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    /**
     * 后台-广告位置列表
     * */
    public function index(){

        //参数部分
        $search_params = $this->request->all();
        $page   = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id',
            'sortedBy'=>'desc',
            'page' => $page,
        ];
        //请求部分
        $ret = http_admin($this->api_url['index'])->send($params);
        //结果处理部分
        $this->httpCodeJudge($ret);
        $data = paginate($this->request, $ret['data']);
        return view('Admin.advertisement.category', $data);
    }
    /**
     * 后台-新增页面
     * */
    public function create(){
        return view('Admin.advertisement.catecreate');
    }
    /**
     * 后台-查看页面
     * */
    public function show(){

    }
    /**
     * 后台-编辑页面
     * */
    public function edit($id){
        $res = http_admin($this->api_url['show'], [$id])->send();

        return view('Admin.advertisement.cateedit', ['cate'=>$res['data']]);
    }
    /**
     * 后台-新增
     * */
    public function store(){
        $this->form();

        $data = $this->request->except('_token','_method');
        $ret = http_admin($this->api_url['store'])->send($data);
        return redirect('/foradmin/advertisingposition');
    }
    /**
     * 后台-编辑
     * */
    public function update($id){

        $this->form();

        $data = $this->request->except('_token','_method');
        $ret = http_admin($this->api_url['update'],[$id])->send($data);
        return redirect('/foradmin/advertisingposition');
    }
    /**
     * 后台-删除
     * */
    public function destroy($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send();
        return redirect('/foradmin/advertisingposition');
    }
    /**
     * 后台-是否显示
     * */
    public function setShow($id){
        $this->validate($this->request, [
            'audit' => 'required'
        ],[
            'audit' => '请选择是否显示'
        ]);

        $data['audit'] = $this->request->get('audit',1);
        $ret = http_admin($this->api_url['setShow'],[$id])->send($data);
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }

    /**
     * 后台-验证保存
     * */
    public function form(){
        $this->validate($this->request,[
            'title' => 'required',
            'audit' => 'required',
            'sort' => 'integer',
        ], [
            'title' => '名称必填',
            'audit' => '是否前台展示必选',
            'sort' => '排序必须为数字',
        ]);

        return $this->request->all();
    }
}